{include file="layout/layui_header" /}

<div class="main-content">
    <div class="page-header">
        <h1 class="page-title">修改密码</h1>
        <p class="page-subtitle">为了账户安全，请定期更换密码</p>
    </div>

    <div class="layui-row layui-col-space20">
        <!-- 主要内容区域 -->
        <div class="layui-col-md8">
            <div class="widget">
                <div class="widget-header">
                    <h2 class="widget-title">密码修改</h2>
                    <div class="widget-actions">
                        <a href="/admin/settings/profile" class="btn-widget">
                            <i class="layui-icon layui-icon-return"></i> 返回个人信息
                        </a>
                    </div>
                </div>

                <form class="layui-form" id="changePasswordForm" lay-filter="changePasswordForm">
                    <div class="layui-row layui-col-space20">
                        <!-- 原密码 -->
                        <div class="layui-col-md12">
                            <div class="layui-form-item">
                                <label class="layui-form-label">
                                    <i class="layui-icon layui-icon-password"></i> 原密码
                                </label>
                                <div class="layui-input-block">
                                    <input type="password" name="old_password" required lay-verify="required" 
                                           placeholder="请输入原密码" class="layui-input" autocomplete="off">
                                </div>
                            </div>
                        </div>

                        <!-- 新密码 -->
                        <div class="layui-col-md12">
                            <div class="layui-form-item">
                                <label class="layui-form-label">
                                    <i class="layui-icon layui-icon-key"></i> 新密码
                                </label>
                                <div class="layui-input-block">
                                    <input type="password" name="new_password" required lay-verify="required|password" 
                                           placeholder="请输入新密码（6位以上）" class="layui-input" autocomplete="off" id="newPassword">
                                    <div class="layui-word-aux" style="margin-top: 5px;">
                                        密码强度：<span id="passwordStrength" class="strength-text">未输入</span>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <!-- 确认密码 -->
                        <div class="layui-col-md12">
                            <div class="layui-form-item">
                                <label class="layui-form-label">
                                    <i class="layui-icon layui-icon-vercode"></i> 确认密码
                                </label>
                                <div class="layui-input-block">
                                    <input type="password" name="confirm_password" required lay-verify="required|confirmPassword" 
                                           placeholder="请再次输入新密码" class="layui-input" autocomplete="off">
                                </div>
                            </div>
                        </div>

                        <!-- 操作按钮 -->
                        <div class="layui-col-md12">
                            <div class="layui-form-item" style="margin-top: 30px;">
                                <div class="layui-input-block">
                                    <button class="layui-btn layui-btn-lg" lay-submit lay-filter="changePassword">
                                        <i class="layui-icon layui-icon-ok"></i> 修改密码
                                    </button>
                                    <button type="reset" class="layui-btn layui-btn-primary layui-btn-lg">
                                        <i class="layui-icon layui-icon-refresh-1"></i> 重置
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>

        <!-- 右侧安全提示 -->
        <div class="layui-col-md4">
            <!-- 安全提示 -->
            <div class="widget">
                <div class="widget-header">
                    <h2 class="widget-title">安全提示</h2>
                </div>
                <div class="security-tips">
                    <div class="tip-item">
                        <i class="layui-icon layui-icon-tips" style="color: #FFB800;"></i>
                        <span>密码长度至少6位字符</span>
                    </div>
                    <div class="tip-item">
                        <i class="layui-icon layui-icon-tips" style="color: #FFB800;"></i>
                        <span>建议包含字母、数字和特殊符号</span>
                    </div>
                    <div class="tip-item">
                        <i class="layui-icon layui-icon-tips" style="color: #FFB800;"></i>
                        <span>不要使用过于简单的密码</span>
                    </div>
                    <div class="tip-item">
                        <i class="layui-icon layui-icon-tips" style="color: #FFB800;"></i>
                        <span>定期更换密码提高安全性</span>
                    </div>
                </div>
            </div>

            <!-- 密码强度说明 -->
            <div class="widget">
                <div class="widget-header">
                    <h2 class="widget-title">密码强度</h2>
                </div>
                <div class="strength-indicators">
                    <div class="strength-level">
                        <div class="strength-bar weak" id="strengthBar"></div>
                        <div class="strength-labels">
                            <span class="weak">弱</span>
                            <span class="medium">中</span>
                            <span class="strong">强</span>
                        </div>
                    </div>
                    <div class="strength-description">
                        <div class="strength-desc-item weak-desc">
                            <strong>弱：</strong>仅包含数字或字母
                        </div>
                        <div class="strength-desc-item medium-desc">
                            <strong>中：</strong>包含字母和数字
                        </div>
                        <div class="strength-desc-item strong-desc">
                            <strong>强：</strong>包含字母、数字和特殊符号
                        </div>
                    </div>
                </div>
            </div>

            <!-- 最近修改记录 -->
            <div class="widget">
                <div class="widget-header">
                    <h2 class="widget-title">最近操作</h2>
                </div>
                <div class="recent-actions">
                    <div class="action-item">
                        <i class="layui-icon layui-icon-user" style="color: #1890ff;"></i>
                        <div class="action-info">
                            <div class="action-title">登录系统</div>
                            <div class="action-time" id="lastLoginTime">正在加载...</div>
                        </div>
                    </div>
                    <div class="action-item">
                        <i class="layui-icon layui-icon-password" style="color: #52c41a;"></i>
                        <div class="action-info">
                            <div class="action-title">密码修改</div>
                            <div class="action-time">暂无记录</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
layui.use(['form', 'layer'], function(){
    var form = layui.form,
        layer = layui.layer,
        $ = layui.jquery;
    
    // 自定义验证规则
    form.verify({
        password: function(value) {
            if (value.length < 6) {
                return '密码长度不能少于6位';
            }
        },
        confirmPassword: function(value) {
            var newPassword = $('#newPassword').val();
            if (value !== newPassword) {
                return '两次输入的密码不一致';
            }
        }
    });
    
    // 密码强度检测
    function checkPasswordStrength(password) {
        var strength = 0;
        var strengthText = '未输入';
        var strengthClass = '';
        
        if (password.length === 0) {
            strength = 0;
            strengthText = '未输入';
            strengthClass = '';
        } else if (password.length < 6) {
            strength = 1;
            strengthText = '太短';
            strengthClass = 'weak';
        } else {
            // 检查是否包含数字
            if (/\d/.test(password)) strength++;
            // 检查是否包含小写字母
            if (/[a-z]/.test(password)) strength++;
            // 检查是否包含大写字母
            if (/[A-Z]/.test(password)) strength++;
            // 检查是否包含特殊字符
            if (/[^A-Za-z0-9]/.test(password)) strength++;
            
            if (strength <= 1) {
                strengthText = '弱';
                strengthClass = 'weak';
            } else if (strength <= 2) {
                strengthText = '中';
                strengthClass = 'medium';
            } else {
                strengthText = '强';
                strengthClass = 'strong';
            }
        }
        
        // 更新强度显示
        $('#passwordStrength').text(strengthText).className = 'strength-text ' + strengthClass;
        $('#strengthBar').removeClass('weak medium strong').addClass(strengthClass);
        
        return strength;
    }
    
    // 监听新密码输入
    $('#newPassword').on('input', function() {
        var password = $(this).val();
        checkPasswordStrength(password);
    });
    
    // 获取个人信息中的最后登录时间
    function loadLastLoginTime() {
        $.ajax({
            url: '/admin/settings/getProfile',
            type: 'GET',
            dataType: 'json',
            success: function(res) {
                if (res.code === 0 && res.data.last_login_time) {
                    $('#lastLoginTime').text(res.data.last_login_time);
                }
            }
        });
    }
    
    // 表单提交
    form.on('submit(changePassword)', function(data) {
        var password = data.field.new_password;
        var strength = checkPasswordStrength(password);
        
        // 弱密码警告
        if (strength <= 1) {
            layer.confirm('您的密码强度较弱，建议使用更复杂的密码。确定要继续吗？', {
                icon: 3,
                title: '密码强度提示'
            }, function(index) {
                layer.close(index);
                submitForm(data.field);
            });
            return false;
        }
        
        submitForm(data.field);
        return false;
    });
    
    // 提交表单
    function submitForm(formData) {
        var loadIndex = layer.load(2);
        
        $.ajax({
            url: '/admin/settings/updatePassword',
            type: 'POST',
            data: formData,
            dataType: 'json',
            success: function(res) {
                layer.close(loadIndex);
                if (res.code === 0) {
                    layer.alert(res.msg || '密码修改成功', {
                        icon: 1,
                        title: '成功'
                    }, function() {
                        // 清空表单
                        $('#changePasswordForm')[0].reset();
                        $('#passwordStrength').text('未输入').removeClass('weak medium strong');
                        $('#strengthBar').removeClass('weak medium strong');
                        layer.closeAll();
                    });
                } else {
                    layer.msg(res.msg || '修改失败', {icon: 2});
                }
            },
            error: function() {
                layer.close(loadIndex);
                layer.msg('网络错误，请稍后重试', {icon: 2});
            }
        });
    }
    
    // 页面加载时获取最后登录时间
    loadLastLoginTime();
});
</script>

<style>
.widget {
    transition: all 0.3s ease;
}

.widget:hover {
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.layui-form-label {
    font-weight: 600;
    color: #333;
    width: 120px;
}

.layui-form-label i {
    margin-right: 5px;
}

.layui-input:focus {
    border-color: #1890ff !important;
    box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
}

/* 安全提示样式 */
.security-tips {
    padding: 10px 0;
}

.tip-item {
    display: flex;
    align-items: center;
    padding: 8px 0;
    color: #666;
    font-size: 14px;
}

.tip-item i {
    margin-right: 8px;
    font-size: 16px;
}

/* 密码强度样式 */
.strength-indicators {
    padding: 10px 0;
}

.strength-level {
    margin-bottom: 15px;
}

.strength-bar {
    height: 6px;
    background: #f0f0f0;
    border-radius: 3px;
    margin-bottom: 10px;
    position: relative;
    overflow: hidden;
}

.strength-bar::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    border-radius: 3px;
    transition: all 0.3s ease;
    width: 0;
}

.strength-bar.weak::after {
    width: 33.33%;
    background: #ff4d4f;
}

.strength-bar.medium::after {
    width: 66.66%;
    background: #faad14;
}

.strength-bar.strong::after {
    width: 100%;
    background: #52c41a;
}

.strength-labels {
    display: flex;
    justify-content: space-between;
    font-size: 12px;
    color: #999;
}

.strength-description {
    font-size: 12px;
    color: #666;
}

.strength-desc-item {
    padding: 3px 0;
}

.strength-text.weak {
    color: #ff4d4f;
}

.strength-text.medium {
    color: #faad14;
}

.strength-text.strong {
    color: #52c41a;
}

/* 最近操作样式 */
.recent-actions {
    padding: 10px 0;
}

.action-item {
    display: flex;
    align-items: center;
    padding: 12px 0;
    border-bottom: 1px solid #f0f0f0;
}

.action-item:last-child {
    border-bottom: none;
}

.action-item i {
    margin-right: 12px;
    font-size: 18px;
}

.action-info {
    flex: 1;
}

.action-title {
    font-weight: 600;
    color: #333;
    margin-bottom: 2px;
}

.action-time {
    font-size: 12px;
    color: #999;
}

.btn-widget {
    transition: all 0.2s ease;
}

.btn-widget:hover {
    transform: translateY(-1px);
    box-shadow: 0 2px 8px rgba(24, 144, 255, 0.3);
}

/* 移动端适配 */
@media (max-width: 768px) {
    .layui-col-md4, .layui-col-md8, .layui-col-md12 {
        width: 100% !important;
        margin-bottom: 15px;
    }
    
    .page-header {
        text-align: center;
    }
    
    .widget-header {
        flex-direction: column;
        text-align: center;
    }
    
    .widget-actions {
        margin-top: 10px;
        width: 100%;
    }
    
    .btn-widget {
        width: 100%;
    }
    
    .layui-form-label {
        width: 100px;
        font-size: 14px;
    }
    
    .action-item {
        padding: 10px 0;
    }
    
    .tip-item {
        font-size: 13px;
    }
}
</style>

{include file="layout/layui_footer" /} 